from django.db import models

# Create your models here.
from mdeditor.fields import MDTextField


class Category(models.Model):
    name = models.CharField(max_length=64, blank=False, null=False, verbose_name="名称")
    desc = models.TextField(blank=True, null=True, max_length=1024, verbose_name="描述")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "分类"
        verbose_name_plural = "分类"


class Tag(models.Model):
    name = models.CharField(max_length=64, blank=False, null=False, verbose_name="名称")
    desc = models.TextField(blank=True, null=True, max_length=1024, verbose_name="描述")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "标签"
        verbose_name_plural = "标签"


class Article(models.Model):
    title = models.CharField(max_length=128, blank=False, null=False, verbose_name="标题")
    tags = models.ManyToManyField(Tag, blank=True, verbose_name="标签")
    category = models.ForeignKey(Category, verbose_name='分类', on_delete=models.CASCADE, blank=False, null=False)
    desc = models.TextField(blank=True, null=True, max_length=1024, verbose_name="描述")
    content = MDTextField(verbose_name="内容")
    views = models.IntegerField(default=0, verbose_name="浏览量")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    thumbnail = models.ImageField(upload_to='thumbnail/', verbose_name="缩略图", blank=True)

    def __str__(self):
        return self.title

    def increase_views(self):
        self.views += 1
        self.save(update_fields=['views'])

    class Meta:
        verbose_name = "文章"
        verbose_name_plural = "文章"
        ordering = ("-updated_at",)


class Comment(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE, verbose_name="文章")
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name="上级评论")
    ip = models.CharField(max_length=64, blank=False, null=False, verbose_name="Ip")
    content = models.TextField(blank=True, null=True, max_length=1024, verbose_name="评论内容")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.content

    class Meta:
        verbose_name = "评论"
        verbose_name_plural = "评论"
        ordering = ("-updated_at",)


class Option(models.Model):
    option_key = models.CharField(max_length=64, verbose_name="key")
    option_value = MDTextField(blank=False, null=False, verbose_name="value")

    def __str__(self):
        return self.option_key

    class Meta:
        verbose_name = "配置项"
        verbose_name_plural = "配置项"
